import argparse
import re
##parser = argparse.ArgumentParser(description ='Process')
##parser.add_argument("a")
def Calculatemain(String):
    if(String.__contains__("(")):
        start = String.rindex("(")
        kuohaoqian = String[0:start]
        kuohaoli = String[start +1:]
        end = kuohaoli.index(")")
        kuohaohou = kuohaoli[end + 1:]
        nowString = kuohaoli[0:end]
        newString = kuohaoqian + str(Calculatemain(nowString)) + kuohaohou
        return Calculatemain(newString)
    else:
        return Calculatemain2(String)
## 模块Calculatemain测试类
##if(String.__contains__("(")):
##    string1 = String.rindex("(")
##    left = String[0:string1]
##    print (left)
##    zhongjian = String[string1 + 1:]
##    print (zhongjian)
##    end = zhongjian.index((")"))
##    right = zhongjian[end + 1:]
##    print (right)
##    nowString = zhongjian[0:end]
##    print (nowString)
##    newString = left + str((nowString)) + right
##    print (newString)
##a = 1+3*6
##print(a)
def Calculatemain2(String1):
    if(String1.__contains__("*")):
        String1 = Calculate2(String1,"*")
        return Calculatemain2(String1)
    if(String1.__contains__("/")):
        String1 = Calculate2(String1,"/")
        return Calculatemain2(String1)
    if(String1.__contains__("+")):
        String1 = Calculate2(String1,"+")
        return Calculatemain2(String1)
    if(String1.__contains__("-")):
        String1 = Calculate2(String1,"-")
        return Calculatemain2(String1)
    return String1
def Calculate2(String1,fuhao):
    start = String1.index(fuhao)
    left = String1[0:start]
    right = String1[start + 1:]
    if(left.isdigit() and right.isdigit() ):
        leftnum = re.search('\d+$',left).group()
        rightnum = re.search('\d',right).group()
        newnum = left.rstrip(leftnum) + str(Calculate(leftnum,rightnum,fuhao)) + right.lstrip(rightnum)
        return newnum
    else:
        print ("error:请输入正数不能输入特殊符号")
def Calculate(num1,num2,fuhao):
    if(fuhao == "+"):
        return (int(num1) + int(num2))
    elif (fuhao == "-"):
        return (int(num1) - int(num2))
    elif (fuhao == "*"):
        return (int(num1) * int(num2))
    elif (fuhao == "/"):
        if(int(num2) == 0):
            return ("分母不能为0")
        else:
            return (int(num1) / int(num2))
    else:
         ("error:请输入正确的运算符")
def yunsuan(String1):    
    return Calculatemain(String1)
##args = parser.parse_args()
##String1 = args.a
##print (yunsuan(String1))
